package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.PayBlack;
import org.dromara.system.domain.vo.PayBlackVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.PayOrderLogBo;
import org.dromara.system.domain.vo.PayOrderLogVo;
import org.dromara.system.domain.PayOrderLog;
import org.dromara.system.mapper.PayOrderLogMapper;
import org.dromara.system.service.IPayOrderLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 订单日志Service业务层处理
 *
 * @author Gavino
 * @date 2024-10-23
 */
@RequiredArgsConstructor
@Service
public class PayOrderLogServiceImpl implements IPayOrderLogService {

    private final PayOrderLogMapper baseMapper;

    /**
     * 保存订单操作日志
     *
     * @param parentId  父级ID
     * @param orderId   订单ID
     * @param orderAid  A订单ID
     * @param operate   操作项
     * @param paramsLog 参数
     * @param returnLog 输出内容
     * @param time      输出内容
     */
    @Override
    public PayOrderLog saveLog(Long parentId, Long orderId, String orderAid, String operate, String paramsLog, String returnLog, Long time) {
       return saveLog(parentId, orderId, null, orderAid, operate, paramsLog, returnLog, time);
    }

    /**
     * 保存订单操作日志
     *
     * @param parentId  父级ID
     * @param orderId   订单ID
     * @param orderNo   订单编号
     * @param orderAid  A订单ID
     * @param operate   操作项
     * @param paramsLog 参数
     * @param returnLog 输出内容
     * @param time      耗时
     */
    @Override
    public PayOrderLog saveLog(Long parentId, Long orderId, String orderNo, String orderAid, String operate, String paramsLog, String returnLog, Long time) {
        try {
            PayOrderLog add = new PayOrderLog();
            if(parentId != null) {
                add.setParentId(parentId);
            }
            if(orderId != null) {
                add.setOrderId(orderId);
            }
            if(StringUtils.isNotBlank(orderNo)) {
                add.setOrderNo(orderNo);
            }
            if(StringUtils.isNotEmpty(orderAid)) {
                add.setOrderAid(orderAid);
            }
            if(StringUtils.isNotBlank(operate)) {
                add.setOperate(operate);
            }
            if(StringUtils.isNotBlank(paramsLog)) {
                add.setParamsLog(paramsLog);
            }
            if(StringUtils.isNotBlank(returnLog)) {
                add.setReturnLog(returnLog);
            }
            if(time != null) {
                add.setTime(time);
            }
            baseMapper.insert(add);
            return add;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存订单操作日志
     *
     * @param logId     日志ID
     * @param orderId   订单ID
     * @param returnLog 输出内容
     * @param time      耗时
     */
    @Override
    public void updateLog(Long logId, Long orderId, String returnLog, Long time) {
        try {
            PayOrderLog update = new PayOrderLog();
            update.setLogId(logId);

            if(orderId != null) {
                update.setOrderId(orderId);
            }
            if(returnLog != null) {
                update.setReturnLog(returnLog);
            }
            if(time != null) {
                update.setTime(time);
            }
            baseMapper.updateById(update);
        }catch (Exception ignore){}
    }

    /**
     * 保存订单操作日志
     *
     * @param logId     日志ID
     * @param paramsLog 参数
     * @param returnLog 输出内容
     * @param time      耗时
     */
    @Override
    public void updateLogParams(Long logId, String paramsLog, String returnLog, Long time) {
        try {
            PayOrderLog update = new PayOrderLog();
            update.setLogId(logId);
            if(StringUtils.isNotBlank(paramsLog)) {
                update.setParamsLog(paramsLog);
            }
            if(returnLog != null) {
                update.setReturnLog(returnLog);
            }
            if(time != null) {
                update.setTime(time);
            }
            baseMapper.updateById(update);
        }catch (Exception ignore){}
    }

    /**
     * 查询订单日志
     *
     * @param logId 主键
     * @return 订单日志
     */
    @Override
    public PayOrderLogVo queryById(Long logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 分页查询支付链接列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 支付链接分页列表
     */
    @Override
    public TableDataInfo<PayOrderLogVo> queryPageList(PayOrderLogBo bo, PageQuery pageQuery){
        LambdaQueryWrapper<PayOrderLog> lqw = buildQueryWrapper(bo);
        Page<PayOrderLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单日志列表
     *
     * @param bo 查询条件
     * @return 订单日志列表
     */
    @Override
    public List<PayOrderLogVo> queryList(PayOrderLogBo bo) {
        LambdaQueryWrapper<PayOrderLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayOrderLog> buildQueryWrapper(PayOrderLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayOrderLog> lqw = Wrappers.lambdaQuery();
        if(Objects.equals(bo.getSortType(), 1)) {
            lqw.orderByAsc(PayOrderLog::getLogId);
        } else if(Objects.equals(bo.getSortType(), 2)) {
            lqw.orderByDesc(PayOrderLog::getLogId);
        }

        lqw.isNull(bo.getQueryParent(), PayOrderLog::getParentId);
        lqw.eq(bo.getParentId() != null, PayOrderLog::getParentId, bo.getParentId());
        lqw.eq(bo.getOrderId() != null, PayOrderLog::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderAid()), PayOrderLog::getOrderAid, bo.getOrderAid());
        lqw.eq(StringUtils.isNotBlank(bo.getOperate()), PayOrderLog::getOperate, bo.getOperate());
        lqw.eq(StringUtils.isNotBlank(bo.getParamsLog()), PayOrderLog::getParamsLog, bo.getParamsLog());
        lqw.eq(StringUtils.isNotBlank(bo.getReturnLog()), PayOrderLog::getReturnLog, bo.getReturnLog());
        lqw.eq(bo.getTime() != null, PayOrderLog::getTime, bo.getTime());
        return lqw;
    }

    /**
     * 新增订单日志
     *
     * @param bo 订单日志
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayOrderLogBo bo) {
        PayOrderLog add = MapstructUtils.convert(bo, PayOrderLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改订单日志
     *
     * @param bo 订单日志
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayOrderLogBo bo) {
        PayOrderLog update = MapstructUtils.convert(bo, PayOrderLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayOrderLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
